package com.carreirati.evaluation.domain;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@RooJavaBean
@RooJpaActiveRecord(versionField = "", table = "project", schema = "dbo")
@RooDbManaged(automaticallyDelete = true)
@RooJson
public class Project {

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
        
        try {
	        Set<KnowledgeArea> knownledgeAreas = this.getKnowledgeAreas1();
	        for (Iterator i = knownledgeAreas.iterator(); i.hasNext(); ) {
	            KnowledgeArea kArea = (KnowledgeArea) i.next();
	            String sqlString = "";
	            sqlString = "INSERT INTO project_knowledge_area VALUES ('" + this.getId() + "','" + kArea.getId() + "')";
	            this.entityManager.createNativeQuery(sqlString).executeUpdate();
	        }
        } catch(Exception e){}
    }

    @Transactional
    public com.carreirati.evaluation.domain.Project merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Project merged = this.entityManager.merge(this);
        this.entityManager.flush();
        String sqlString = "";
        sqlString = "DELETE project_knowledge_area WHERE project_id = '" + this.getId() + "'";
        this.entityManager.createNativeQuery(sqlString).executeUpdate();
        Set<KnowledgeArea> knownledgeAreas = this.getKnowledgeAreas1();
        for (Iterator i = knownledgeAreas.iterator(); i.hasNext(); ) {
            KnowledgeArea kArea = (KnowledgeArea) i.next();
            sqlString = "";
            sqlString = "INSERT INTO project_knowledge_area VALUES ('" + this.getId() + "','" + kArea.getId() + "')";
            this.entityManager.createNativeQuery(sqlString).executeUpdate();
        }
        return merged;
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            String sqlString = "";
            sqlString = "DELETE project_knowledge_area WHERE project_id = '" + this.getId() + "'";
            this.entityManager.createNativeQuery(sqlString).executeUpdate();
            this.entityManager.remove(this);
        } else {
            Project attached = Project.findProject(this.getId());
            this.entityManager.remove(attached);
        }
    }
    
    public static List<Project> findByKnowledgeArea(long knowledgeAreaId) {
    	String sqlString = "SELECT p.* FROM project p  INNER JOIN project_knowledge_area pka  ON p.id = pka.project_id ";
    		sqlString += " WHERE knowledge_area_id = '"+  knowledgeAreaId + "'" +
    				" ORDER BY name";
        return entityManager().createNativeQuery(sqlString, Project.class).getResultList();
    }
}
